
package cup.modules.visual.dynamic;

import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * 数据库工具类
 *
 *
 */
@Component
public class DynamicDataSourceHelper {

	/**
	 * 测试数据库链接
	 */
	public Boolean dbTest(String driverClass, String url, String username, String password) {
		Connection conn = null;
		try {

			url=StringEscapeUtils.unescapeHtml(url);
			//测试驱动类
			Class.forName(driverClass);
			//创建连接
			conn = DriverManager.getConnection(url, username, password);
			conn.setAutoCommit(Boolean.FALSE);
			return true;
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		} finally {
			//关闭连接
			dbClose(conn);
		}
	}

	/**
	 * 关闭数据库链接
	 */
	private void dbClose(Connection conn) {
		try {
			//关闭数据源连接
			if (conn != null) {
				conn.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

}
